﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;
using System.Web.SessionState;

namespace jnlamp.Web.devinfo
{
    /// <summary>
    /// GetDays 的摘要说明
    /// </summary>
    public class GetDays : IHttpHandler, IRequiresSessionState
    {
        jnlamp.BLL.devdays bll = new jnlamp.BLL.devdays();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "querywork")
                QueryWork(context);
            //else if (ac == "ddllist")
            //    ddllist(context);
        }
        public void QueryWork(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string iswork = "2", sdate = "", edate = "", devtype = "", sebm = "", DevLocation="";
                DateTime sdatet = DateTime.Now, edatet = DateTime.Now;
                if (null != context.Request.Form["utype"])
                {
                    iswork =context.Request.Form["utype"].ToString().Trim();
                }
                if (null != context.Request.Form["devtype"])
                {
                    devtype = context.Request.Form["devtype"].ToString().Trim();
                }
                if (null != context.Request.Form["sdate"])
                {
                    sdate = context.Request.Form["sdate"].ToString().Trim();
                    sdatet = DateTime.Parse(sdate);
                }
                if (null != context.Request.Form["edate"])
                {
                    edate = context.Request.Form["edate"].ToString().Trim();
                    edatet = DateTime.Parse(edate);
                }
                if (null != context.Request.Form["sebm"])
                {
                    sebm = context.Request.Form["sebm"].ToString().Trim();
                }
                if (sebm != "" && sebm != "0")
                {                    
                }
                else
                {
                    string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                    DevLocation = string.Join("", pcc);
                    //strWhere.AppendFormat(" DevLocation like '%{0}%' and ", add);

                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }

                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                string where = "";
                if (iswork != "2")
                {
                    where = "devok='"+iswork+"'";
                }
                DataSet ds = bll.GetDevWork(sdatet, edatet, devtype,true,sebm,where,oderby,DevLocation);
                int count =0;
                if (ds != null) count = ds.Tables[0].Rows.Count;
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }  
        //public void ddllist(HttpContext context)
        //{
        //    context.Response.ContentType = "text/plain";
        //    int uid = Web.Admin.myinfo.getID(context);
        //    if (uid != 0)
        //    {
        //        StringBuilder strWhere = new StringBuilder();
        //        strWhere.AppendFormat(" uid={0} ", uid);
        //        DataSet ds = bll.GetList(strWhere.ToString());
        //        string strJson = ToJson.DataTable2Json1(ds.Tables[0]);//DataSet数据转化为Json数据  
        //        context.Response.Write(strJson);//返回给前台页面  
        //        context.Response.End();
        //    }
        //    else
        //    {
        //        context.Response.Write("L");//返回给前台页面  
        //        context.Response.End();

        //    }
        //}
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["ddid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string mname = context.Request.Form["devtype"];
                string days = context.Request.Form["interday"];
                string ddids = context.Request.Form["ddid"];
                int day = 0;
                int ddid = 0;
                int.TryParse(days, out day);
                int.TryParse(ddids, out ddid);
                jnlamp.Model.devdays model = new jnlamp.Model.devdays();
                model.devtype = mname;
                model.interday = day;
                model.ddid = ddid;
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string mname = context.Request.Form["devtype"];
                string days = context.Request.Form["interday"];
                int day = 0;
                int.TryParse(days, out day);
                if (day != 0)
                {
                    jnlamp.Model.devdays model = new jnlamp.Model.devdays();
                    model.devtype = mname;
                    model.interday = day;
                    if (bll.Add(model) > 0)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {               
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                               
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage("", oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount("");//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }  

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}